// import appCache from "../app/app.js";
import appCache from "./app.js";
import config from "./config.js";
export default {
  userCode: null,
  userAuthorization: null,

  setEngine(obj) {
    engine = obj;
  },

  checkEngine() {
    // 检查本地数据
    let serveMode = engine.getAccountCache(KEY_SERVERMODE);
    if (serveMode) {
      if (engine.getDebugServer() != serveMode) {
        engine.clearAccountCache();
        if (engine.clearPlatformCache) {
          engine.clearPlatformCache();
        }
      }
    } else {
      if (engine.getAccountCache(KEY_CODE)) {
        engine.clearAccountCache();
        if (engine.clearPlatformCache) {
          engine.clearPlatformCache();
        }
      }
    }
  },

  getApp: () => appCache,

  getAppConfig: () => config,

  isDebugMode: () => config.isDebugMode(),

  getDebugServer: () => config.getDebugServer(),

  getMapKey: () => config.getMapKey(),

  clearAppCache: () => {
    engine.userCode = null;
    engine.userAuthorization = null;
    appCache.clearAccountCache();
  },

  clearPlatformCache: (callback) => {
    appCache.clearAppCache(callback);
  },

  setPlatformCache: (key, value) => {
    appCache.setAppCache(key, value);
  },

  removePlatformCache: (key) => {
    appCache.removeAppCache(key);
  },

  setClipboardData: (text, callback) => {
    appCache.setClipboardData(text, callback);
  },

  getClipboardData: (callback) => {
    appCache.getClipboardData(callback);
  },

  getLocation: (isShowLoading, param, callback) => {
    appCache.getLocation(isShowLoading, callback);
  },

  handleRequestOverdue() {
    appCache.clearAppCache(function () {
      appCache.jumpLoginPage();
    });
  },

  handleRequestLogin(callback) {
    if (callback) {
      callback(null);
    }
    appCache.jumpLoginPage();
  },

  login(tempToken, callback) {
    engine.requestUrl(true, engine.getAppConfig().getNetHeadUrl() + "/managerlogin/gettoken", "POST", tempToken, true).then(function (data) {
      if (data && data.userId && data.token) {
        engine.saveAccount({
          userId: data.userId,
          token: data.token,
          // avatarUrl: data.userUrl,
          // nickName: data.userName,
          // phone: data.userPhone,
        });
        let roleList = null;
        if (data.role) {
          for (let i = 0; i < data.role.length; i++) {
            let roleItem = data.role[i];
            if (roleItem) {
              if (!roleList) {
                roleList = [];
              }
              roleList.push({
                name: roleItem.nickName,
                entityName: roleItem.enterpriseCode,
                linkId: roleItem.linkID,
                roleId: roleItem.role,
                roleName: roleItem.roleName,
                areaInfo: roleItem.rosideArea
              });
            }
          }
        }
        callback(true, roleList);
      } else {
        callback(false, "result error");
      }
    }).catch(function (reason) {
      callback(false, reason);
    });
  },

  getAccount(callback) {
    if (!engine.userCode || !engine.userAuthorization) {
      engine.userCode = engine.getAccountCache(KEY_CODE);
      engine.userAuthorization = engine.getAccountCache(KEY_TOKEN);
      if (!engine.userCode || !engine.userAuthorization) {
        engine.handleRequestLogin(callback);
        return;
      }
    }
    if (callback) {
      callback({
        code: engine.userCode,
        token: engine.userAuthorization
      });
    }
  },

  getAccountCodeSync() {
    if (engine.userCode) {
      return engine.userCode;
    }
    engine.userCode = engine.getAccountCache(KEY_CODE);
    return engine.userCode;
  },

  getMyNameSync() {
    return engine.getAccountCache(KEY_NICKNAME);
  },

  getMyIconSync() {
    return engine.getAccountCache(KEY_AVATARURL);
  },

  getMyPhoneSync() {
    return engine.getAccountCache(KEY_PHONE);
  },

  getDestPlatformSync() {
    return engine.getAccountCache(KEY_DESTPLATFORM);
  },

  saveAccount(data) {
    engine.userCode = data.userId;
    engine.userAuthorization = data.token;
    engine.setAccountCache(KEY_CODE, engine.userCode);
    engine.setAccountCache(KEY_TOKEN, engine.userAuthorization);
    engine.setAccountCache(KEY_SERVERMODE, engine.getDebugServer());
    engine.setAccountCache(KEY_AVATARURL, data.avatarUrl);
    engine.setAccountCache(KEY_NICKNAME, data.nickName);
    engine.setAccountCache(KEY_PHONE, data.phone);
    engine.setAccountCache(KEY_DESTPLATFORM, data.destPlatform);
  },

  getRequestHeader() {
    let header = {
      "content-type": "application/json"
    };
    header.from = 0;
    header.source = 5;
    if (engine.userCode && engine.userAuthorization) {
      header.authdst = engine.userCode;
      header.userid = engine.userCode;
      header.Authorization = engine.userAuthorization;
    }
    let userValue = appCache.getAccountValueSync();
    if (userValue && userValue.linkId) {
      header.LinkID = userValue.linkId;
    }
    return header;
  },

  getUploadFileHeader() {
    let header = {
      "content-type": "multipart/form-data" // application/x-www-form-urlencoded
    };
    header.from = 0;
    header.source = 5;
    return header;
  }
}
var engine = null;
const KEY_CODE = "a";
const KEY_TOKEN = "b";
const KEY_SERVERMODE = "c";
const KEY_AVATARURL = "d";
const KEY_NICKNAME = "e";
const KEY_PHONE = "f";
const KEY_DESTPLATFORM = "g";